<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <style>
        /* .box{
            margin: 0 auto;
            padding: 0;
        } */
        .wrapper{
            width: 1200px;
        }
        .lf {
            float: left;
        }
        .top{
            background-color: gray;
            height: 40px;
            color: white;
            line-height: 40px;
        }
        .list{
            border-top: 2px solid black;
            border-bottom: 2px solid black;
        }
        .produce{
            width: 150px;
            height: 150px;
            background-color:wheat;
        }
    </style>
</head>
<body>
    <script src="./js/jquery.min.js"></script>

    <div class=" container wrapper">
        <h1>购物车案例</h1>

        <div class="row top">
            <div class="col-md-3 ">
                <input type="checkbox" class="allSel">全选
                商品
            </div>
        </div>

        <div class="row list">

            <div>
                <input type="checkbox" class="pro">
            </div>
            <div class="produce">商品</div>
            <div>
                <p>
                    `价格实惠------------
                    -------------------·
                </p>
            </div>
            <div>
                <span>价格</span>
                <span>￥</span>
                <span class="price">18</span>
            </div>

            <div>
                <span>数量</span>
                <span class="addSpan">+</span>
                <input type="number" value="1" class="inp">
                <span class="subSpan">-</span>
            </div>

        </div>


        <div class="row top">
            <div class="col-md-3 ">
                <input type="checkbox" class="allSel">全选
                商品
            </div>
            <div><span class="allSelNum"></span> 购物车 ￥<span class="sumPrice"></span></div>
        </div>
        
    </div>
</body>
<script>
    $( function() {

        let sum = 0;

         $('.sumPrice').text(sum);

        $('.inp').css('width', 30);
        $('.allSel').change( function() {
            //选中所有商品
            $('.pro').prop('checked', $(this).prop('checked'));
        })
        $('.pro').click( function() {
            if ($('.pro:checked').length == $('.pro').length) {

                $('.allSel').prop('checked', true);
                let index = $(this).index();
                
                sum += parseInt( $('.price').text().toString() );
                console.log($('.price').text().toString());
                $('.sumPrice').text(sum.toString());

            }else{
                $('.allSel').prop('checked', false);

            }
        })
        $('.addSpan').click( function() {
            let num = $(this).siblings('.inp').val();
            $(this).siblings('.inp').val(++num);
        })
        $('.subSpan').click(function () {
            let num = $(this).siblings('.inp').val();
            $(this).siblings('.inp').val(--num);
        })
    })
</script>
</html>